package com.hy.chat.client.view;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.Loader;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Theme;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.Layout;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HtmlEditor;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dev.jjs.impl.GwtAstBuilder;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.hy.chat.client.Chat;
import com.hy.chat.client.ChatService;
import com.hy.chat.client.ChatServiceAsync;
import com.hy.chat.client.presenter.AdminChatPersenter.Display;
import com.hy.chat.client.widget.ChatTabItem;
import com.hy.chat.client.widget.ChatWindow;



public class AdminChatView extends DecoratorPanel implements Display{
	
	
	final Button sentButton = new Button("发送");
	ChatWindow window = new ChatWindow("在线服务");
	final TabPanel folder = new TabPanel();  
	final HtmlEditor htmlEditor = new HtmlEditor();  

	/*private final ChatServiceAsync greetingService = GWT
			.create(ChatService.class);*/
	private ChatServiceAsync greetingService= Registry.get(Chat.CHAT_SERVICE);

	private static final String  ICON = "myGwt-image1";
	
	public AdminChatView() {
		System.out.println("********* this is AdminChat View .");
			
			ContentPanel contentPanel = new ContentPanel();
			contentPanel.setHeaderVisible(false);
			
			    folder.setHeight(250);  
			  
//			    ChatTabItem chatTabItem = new ChatTabItem("tabItem");  
//			    chatTabItem.addStyleName("pad-text");  
//			    chatTabItem.setHeight(100);  
//			    chatTabItem.setData("messageIndex", 0);
//			    chatTabItem.setScrollMode(Scroll.AUTOY); 
//			    Html html = chatTabItem.addText("tabItem<br>");  
			    ChatTabItem chatTabItem = this.createChatTabItem("使用说明", "使用说明 ");
			    
			    folder.add(chatTabItem);  
			    		    	    
			    folder.setTabScroll(true);  
			    folder.setCloseContextMenu(true);  
			contentPanel.add(folder);
			this.configureHtmlEditor(htmlEditor);

		    
		    	
		    	
		    	
			    contentPanel.add(htmlEditor, new FormData("100% -53"));  

			    contentPanel.addButton(sentButton);
			   	contentPanel.setButtonAlign(HorizontalAlignment.RIGHT);
		
//		window.add(folder);
		window.add(contentPanel);
		this.add(window);
	}
	
	
	
	
	
	
	private ChatTabItem createChatTabItem (String sessionId,String text) {
		ChatTabItem chatTabItem = new ChatTabItem(sessionId);  
		    chatTabItem.addStyleName("pad-text");  
		    chatTabItem.setHeight(100);  
		    chatTabItem.setData("messageIndex", 0);
		    chatTabItem.setScrollMode(Scroll.AUTOY); 

		    Html html = chatTabItem.addText(text);  
		    
		    return chatTabItem;
	}
	
	private ChatTabItem createChatTabItem (String sessionId,String message,int index) {
		
		    return this.createChatTabItem(sessionId, message, index, false);
	}
	
	private ChatTabItem createChatTabItem (String sessionId,String message,int index,boolean closable) {
		ChatTabItem chatTabItem = new ChatTabItem("与客户"+sessionId.substring(0, 6)+"会话");  
		    chatTabItem.addStyleName("pad-text");  
		    chatTabItem.setHeight(100);  
		    chatTabItem.setIconStyle(ICON);

		    chatTabItem.setData("messageIndex", index);
		    chatTabItem.setData("sessionId", sessionId);
		    chatTabItem.setClosable(closable);
//		    chatTabItem.addText(message);  
		    chatTabItem.setScrollMode(Scroll.AUTOY); 

		    Html xx =chatTabItem.addText(message);  
		   // chatTabItem.focus();
		    return chatTabItem;
	}
	
	private String getCurrentDateTime() {
		return DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT).format(new Date());
	}
	
	private HtmlEditor configureHtmlEditor(HtmlEditor htmlEditor) {
		htmlEditor.setHeight( 100);
		htmlEditor.setWidth(550);
		htmlEditor.setHideLabel(true);  
		return htmlEditor;
	}
	
	
	

	@Override
	public Button getSentButton() {
		return sentButton;
	}

	@Override
	public TabPanel getTabPanel() {
		
		return folder;
	}

	@Override
	public HtmlEditor getHtmlEditor() {
		return htmlEditor;
	}



}
